<?php

/**
 * Controller para tratar requisições ajax
 *
 * @author Igor Rocha <igortice@gmail.com>
 * @copyright Copyright (c) 2011, Igor Rocha
 */
class AjaxController extends BaseController
{

    /**
     * Action reponsavel por montar a estrutura de pastas
     */
    public function getfoldersAction()
    {
        $this->ajaxNoRender();

        if ($this->isPostAjax()) {
            $alfresco = array('url' => $this->getUrlAlfresco(), 'ticket' => $this->getTicket());

            if ($this->_getParam('dir') === '/')
                echo $this->_helper->folder($alfresco)->getRootFolders(Zend_Registry::get('folderview'));
            else {
                $url = explode('/', $this->_getParam('dir'));
                $id = $url[2];

                echo $this->_helper->folder($alfresco)->getChildren($id);
            }
        }
    }

    public function getdocumentsAction()
    {
        $this->_helper->layout->disableLayout();
        $id = $this->_request->getParam('id');
        $alfresco = new Alfresco_Rest_SpacesStore($this->getUrlAlfresco(), $this->getTicket());

        if ($this->_request->getParam('document')) 
            $this->view->document = $this->_request->getParam('document');

        $service = new Ged_Service_TipoDocumento($this->getTicket());
        $this->view->tiposdocumentos = $service->buscar('1');
        $this->view->parents = $alfresco->getAllParents($id);
        $this->view->folder = $alfresco->getNode($id);
        $this->view->childrens = $alfresco->getChildren($id);
    }
    
    public function updateArquivosAction()
    {
        $this->ajaxNoRender();
        $postData = array();
        if ($this->isPostAjax()) {
            $postData = $this->_formatArrayJson();
            $arquivosService = new Ged_Service_Arquivo($this->getTicket());
            $arquivosService->updateArquivos($postData);
        }
    }
    
    private function _formatArrayJson()
    {
        $json = array();
        $json['raiz'] = array('tipo_documento' => $this->_request->getParam('tipo_documento'),
            'ids_arquivos'  => $this->_request->getParam('ids_arquivos'));
        return $json;
    }

}
